package cn.jjxx.modules.task.task;

import cn.jjxx.core.utils.SpringContextHolder;
import cn.jjxx.core.utils.http.HttpRequestUtils;
import cn.jjxx.modules.pb.entity.*;
import cn.jjxx.modules.pb.service.*;
import cn.jjxx.modules.record.entity.DevelopmentOfMembers;
import cn.jjxx.modules.record.entity.PartyOrganization;
import cn.jjxx.modules.record.service.IDevelopmentOfMembersService;
import cn.jjxx.modules.record.service.IPartyOrganizationService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class PartyDevelopmentSync {

    public final Logger log = Logger.getLogger(this.getClass());

    public void run() {
        String url ="http://www.gcypt.com/lqdj/bigscreen/getOrgInfoReport";
        JSONObject json = HttpRequestUtils.httpGet(url);
        Map<String,Object> data =(Map<String,Object>)json.get("data");
        Object partyOrgJson = data.get("djOrgTypeCount");
        PartyOrganization partyOrganization = PartyOrganization.fromObject(partyOrgJson);
        IPartyOrganizationService partyOrganizationService = SpringContextHolder.getBean("partyOrganizationService");
        partyOrganizationService.delete(null);
        partyOrganizationService.insert(partyOrganization);

        Object membersDevelopJson =data.get("stageGroup");
        DevelopmentOfMembers developmentOfMembers = DevelopmentOfMembers.fromObject(membersDevelopJson);
        IDevelopmentOfMembersService developmentOfMembersService = SpringContextHolder.getBean("developmentOfMembersService");
        developmentOfMembersService.delete(null);
        developmentOfMembersService.insert(developmentOfMembers);

        Map<String,Integer> sexMap = (Map<String,Integer>)data.get("sexGroup");
        PbSexRate sexRate = new PbSexRate();
        sexRate.setMen(sexMap.get("men"));
        sexRate.setWomen(sexMap.get("women"));
        IPbSexRateService sexRateService = SpringContextHolder.getBean("pbSexRateService");
        sexRateService.delete(null);
        sexRateService.insert(sexRate);

        List<Map<String,Object>> eduList = (List<Map<String,Object>>)data.get("eduGroup");
        List<PbEducation> eduEntityList = new ArrayList<>();
        int sort =1;
        for(Map<String,Object> map:eduList){
            PbEducation entity = new PbEducation();
            entity.setEdu(map.get("edu").toString());
            entity.setNum((Integer)map.get("num"));
            entity.setSort(sort++);
            eduEntityList.add(entity);
        }
        IPbEducationService pbEducationService = SpringContextHolder.getBean("pbEducationService");
        pbEducationService.delete(null);
        pbEducationService.insertBatch(eduEntityList);

        List<Map<String,Integer>> ageList = (List<Map<String,Integer>>)data.get("ageGroup");
        List<PbAge> ageEntityList = new ArrayList<>();
        Map<String,Integer> ageMap=ageList.get(0);
        for(Map.Entry<String,Integer> e:ageMap.entrySet()){
            PbAge age = new PbAge();
            age.setLabel(e.getKey());
            age.setAmount(e.getValue());
            ageEntityList.add(age);
        }
        IPbAgeService pbAgeService = SpringContextHolder.getBean("pbAgeService");
        pbAgeService.delete(null);
        pbAgeService.insertBatch(ageEntityList);

        List<Map<String,Object>> zzshGroup = (List<Map<String,Object>>)data.get("zzshGroup");
        List<PbPoliticalLife> politicalEntityList = new ArrayList<>();
        for(Map<String,Object> map:zzshGroup){
            PbPoliticalLife politicalLife = new PbPoliticalLife();
            politicalLife.setType(map.get("type").toString());
            SimpleDateFormat sdf = new SimpleDateFormat();
            politicalLife.setMonth(map.get("month").toString());
            politicalLife.setNum((Integer)map.get("num"));
            politicalEntityList.add(politicalLife);
        }
        IPbPoliticalLifeService pbPoliticalLifeService =SpringContextHolder.getBean("pbPoliticalLifeService");
        pbPoliticalLifeService.delete(null);
        pbPoliticalLifeService.insertBatch(politicalEntityList);

        url = "http://www.gcypt.com/lqdj/bigscreen/countByYearAndMonth";
        json = HttpRequestUtils.httpGet(url);
        data =(Map<String,Object>)json.get("data");
        List<Map<String,Object>> monthCount = (List<Map<String,Object>>)data.get("monthCount");
        List<PbPartyFee> feeEntityList = new ArrayList<>();
        for(Map<String,Object> map:monthCount){
            PbPartyFee partyFee = new PbPartyFee();
            partyFee.setSumOfYear(data.get("sumOfYear").toString());
            partyFee.setMonth(map.get("month").toString());
            partyFee.setSum((int)Float.parseFloat(map.get("sum").toString()));
            feeEntityList.add(partyFee);
        }
        IPbPartyFeeService pbPartyFeeService = SpringContextHolder.getBean("pbPartyFeeService");
        pbPartyFeeService.delete(null);
        pbPartyFeeService.insertBatch(feeEntityList);


        url ="http://www.gcypt.com/lqdj/bigscreen/getOrgCheckResult";
        json = HttpRequestUtils.httpGet(url);
        data =(Map<String,Object>)json.get("data");
        List<PbCheck> checkEntityList = PbCheck.fromJsonObject(data);
        IPbCheckService pbCheckService = SpringContextHolder.getBean("pbCheckService");
        pbCheckService.delete(null);
        pbCheckService.insertBatch(checkEntityList);

        log.info("Party building data sync succeed.");
    }
}
